<template>
  <div>
    <!-- 获利分析代码维护 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhProfitAnalysisCodeMaintain"
      :columns="columns"
      :isSelectColumn="false"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <multiuse-button size="small" type="primary" @click="addHandle(2)">新增</multiuse-button>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="visible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SonModel :type="handleType" :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, listOrderType } from '@/api/omFinance/PriceDiscountManage/ProfitAnalysisCodeMaintain';
import SonModel from './SonModel';

export default {
  name: 'ProfitAnalysisCodeMaintain',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      visible: false,
      handleType: '',
      drawerTitle: ''
    };
  },
  components: {
    SonModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.getAllOrderType();
  },
  methods: {
    async getAllOrderType() {
      const res = await listOrderType();
      if (res.resultCode == 200) {
        this.columns.find(x => x.dataIndex == 'orderTypeSubId').editItems = res.data.map(x => ({ text: x.subName, value: x.id }));
        this.columns.find(x => x.dataIndex == 'orderTypeSubId').filterItems = res.data.map(x => ({ text: x.subName, value: x.id }));
      }
    },
    // 订单细分
    async getOrderType(orderType) {
      const res = await listOrderType({ orderType: orderType });
      if (res.resultCode == 200) {
        this.topFilterList.find(item => item.fieldName == 'orderTypeSubId').itemList = res.data.map(x => ({ text: x.subName, value: x.id }));
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    selectChangeHandle(row) {},

    addHandle() {
      this.handleType = 'add';
      this.formData = {};
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    detailHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    emitCloseHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('6006'),
          change: val => {
            if (val) {
              this.topFilterList.find(x => x.fieldName == 'orderTypeSubId').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderTypeSubId').initialValue = '';
              this.getOrderType(val);
              return;
            }
            this.topFilterList.find(x => x.fieldName == 'orderTypeSubId').itemList = [];
            this.topFilterList.find(x => x.fieldName == 'orderTypeSubId').initialValue = '';
          }
        },
        {
          type: 'SELECT',
          label: '订单细分',
          fieldName: 'orderTypeSubId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '销售类型',
          fieldName: 'salesType',
          placeholder: '请选择',
          itemList: this.createDictList('6053')
        },
        {
          type: 'SELECT',
          label: '订单原因',
          fieldName: 'orderReason',
          placeholder: '请选择',
          itemList: this.createDictList('6054')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '销售订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: props => {
            return <span>{this.createDictText(props.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '订单细分名称',
          dataIndex: 'orderTypeSubId',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '销售类型',
          dataIndex: 'salesType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6053'),
          render: props => {
            return <span>{this.createDictText(props.row.salesType, '6053')}</span>;
          }
        },
        {
          title: '订单原因',
          dataIndex: 'orderReason',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6054'),
          render: props => {
            return <span>{this.createDictText(props.row.orderReason, '6054')}</span>;
          }
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '创建人',
          dataIndex: 'createdByName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
